local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local event_manager = _G.ServerOHOH.event_manager;
local EventList = _G.ServerOHOH.load.EventList();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.player=self.parent.player
	self.guid=self.parent:GetGuid()

	self.weight=self.parent.weight
	self.drop=self.parent.drop	
	
	self.itemCtrl=self.player:GetComponent(_G.ServerOHOH.load.Server_ItemCtrl())
	self.parent.DieEvent:Connect(function()
		local drop=self:GetDrop()
		local playerCtrl=self.player:GetComponent(_G.CommonOHOH.load.Common_PlayerToolCtrl())
		local data=playerCtrl:GetDrop(drop)
		self.itemCtrl:AddItem(data)
		self:Fire(data)
	end)
end;

--计算luck值
function M.GetLuckyValue(self)

	local baseList=self.player.LuckyValueBaseEvent:Action()
	local mulList= self.player.LuckyValueMulEvent:Action()
	local addList= self.player.LuckyValueAddEvent:Action()

	local base=0
	for _,v in pairs(baseList) do
		 base=base+v
	end
 
	local mul=1
	for _,v in pairs(mulList) do
		 mul=mul+v
	end

	local add=0
	for _,v in pairs(addList) do
		  add=add+v
	 end
	
	 local v=base*mul+add
 	return v
end


--计算掉落物
function M.GetDrop(self)
	local luckyValue=self:GetLuckyValue()
	luckyValue=luckyValue or 0
	local w=self.weight or {}
	local index=GeneralTool.RandomLucky(w,luckyValue)
	return self.drop[index]
end

function M.Fire(self,drop)
	event_manager:Fire(self.player,EventList.trian_drop,self.guid,drop)
end

return M